package com.ym.common.validator;

import com.ym.common.annotations.CustomConstraint;
import com.ym.common.constant.DictEnum;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CustomEnumValidator implements ConstraintValidator<CustomConstraint, String> {

	private Class<? extends DictEnum> clazz;

	@Override
	public void initialize(CustomConstraint arg0) {
		clazz = arg0.value();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext arg1) {

		if (StringUtils.isBlank(value)) {
			return true;
		}
		return DictEnum.exist(clazz, value);
	}

}
